var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifShowPasswordTips:false,
    buthidden: false,
    moneyNum:"",
    amount:"",
    bankCardName:"",
    bankCardNumber:"",
    bankCard:"",
    payStatus:"",
    ifTrue: false,
    Length: 6,        //输入框个数
    isFocusOne: true,    //聚焦
    ValueOne: "",        //输入的内容
    ispassword: true, //是否密文显示 true为密文， false为明文。
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
    that.setData({ 
      amount: options.amount,
      // bankCardName: options.bankCardName,
      // bankCardNumber: options.bankCardNumber,
      // bankCard: options.bankCardNumber.substr(-4),
      // payStatus: options.payStatus,
      // wxType: options.wxType,
      // phone: options.phone
    });
  },

  //提现金额验证
  onNumberMoney: function (e) {
    var that = this;
    var price = e.detail.value;
    if (!/^(([1-9]\d*[.]\d\d?)|([0][.]\d\d?)|([1-9]\d*)|0)$/.test(price)) {
      price = /[1-9]\d*[.]\d\d?/.exec(e.detail.value);
      price = price ? price : /[0][.]\d\d?/.exec(e.detail.value);
      price = price ? price : /[1-9]\d*/.exec(e.detail.value);
      price = (price == '' || price == null ? '' : price);
      price = /^[^.]+[.]$/.test(e.detail.value) ? price + "." : price;
    }
    if(price!=""){
      that.setData({
        moneyNum: price,
      });
    }

  },
  onSubmit:function(){
    var that = this;
    var amount = Number(that.data.amount).toFixed(2);
    var moneyNum = Number(that.data.moneyNum).toFixed(2);
    var payStatus = that.data.payStatus;
   
      if (payStatus == 1) {
        if (!moneyNum) {
          wx.showToast({
            title: '请输入金额',
            icon: 'none',
            duration: 1500
          })
          return false;
        } else if (parseFloat(moneyNum) > parseFloat(amount)) {
          wx.showToast({
            title: '余额不足',
            icon: 'none',
            duration: 1500
          })
          return false;

        } else if (moneyNum < 10) {
          wx.showModal({
            title: '最低提现金额为10元',
            confirmColor: '#56BDE0',
            showCancel: false,
          });
          return false;
        } else if (that.data.ValueOne == "") {
          // wx.showToast({
          //   title: '支付密码必填',
          //   icon: "none",
          //   duration: 1500
          // })
          that.setData({ ifShowPasswordTips: true, });
          return false;
        } else if (that.data.ValueOne.length != 6) {
          wx.showToast({
            title: '密码长度有误',
            icon: "none",
            duration: 1500
          })
          return false;
        } else {
       // that.setData({ ifShowPasswordTips: true });
        that.setData({ buthidden: true });
        wx.request({
          method: "POST",
          url: 'https://www.zaishengwang.com/wx/toPutForward.do',
          dataType: 'json',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            userId: app.globalData.userId,
            amount: moneyNum,
            password: that.data.ValueOne
          },
          success: function (res) {
            
            var c = 2;
            if (res.data.msg == "1") {
              wx.showToast({
                title: '提现申请成功',
                icon: 'success',
                duration: 1500
              });
              // var intervalId = setInterval(function () {
              //   c = c - 1;
              //   if (c == 0) {
              //     clearInterval(intervalId);
              //     wx.navigateBack({
              //       delta: 1
              //     })
              //   }
              // }, 1000)

              var createTime = res.data.bankcardInfoVo.createTime;
              var bankCardName = res.data.bankcardInfoVo.bankCardName;
              var bankCardNumber = res.data.bankcardInfoVo.bankCardNumber;
              var price = res.data.bankcardInfoVo.price;
               var intervalId = setInterval(function () {
                c = c - 1;
                if (c == 0) {
                  clearInterval(intervalId);
                  wx.navigateTo({
                    url: 'cashProgress?bankCardName=' + bankCardName + '&bankCardNumber=' + bankCardNumber + '&price=' + price + '&createTime=' + createTime,
                  })
                }
              }, 1000)
            
            } else if (res.data.msg == "2"){
              // wx.showToast({
              //   title: '密码错误',
              //   icon: 'none',
              //   duration: 1500
              // });
              wx.showModal({
                title: '提示',
                content: '支付密码错误',
                cancelText: "忘记密码",
                confirmText: "重试",
                cancelColor: "#50BEC5",
                confirmColor: "#666",
                success(res) {
                  if (res.confirm) {
                    that.setData({ ifShowPasswordTips: true, ValueOne: "" });//重试
                  } else if (res.cancel) {
                    that.goCheckSetPaymentPassword();//忘记密码
                  }
                }
              })
              //that.setData({ ifShowPasswordTips: true, ValueOne:""});
              that.setData({ buthidden: false });
            } else {
              that.setData({ buthidden: false });
            }

          },
          fail: function (error) {
            console.log(error);
            that.setData({ buthidden: false });
          }

        })
        }
      } else if (payStatus == 2) {
        wx.showModal({
          title: '提示',
          content: '为了保障您的资金安全，请设置支付密码用于后续余额提现或使用',
          cancelText: "取消",
          confirmText: "去设置",
          cancelColor: "#666",
          confirmColor: "#50BEC5",
          success(res) {
            if (res.confirm) {
              
              wx.navigateTo({
                url: '../mine/checkSetPaymentPassword?wxType=' + that.data.wxType + '&phone=' + that.data.phone + '&payStatus=' + that.data.payStatus,
              })
            } else if (res.cancel) {
             
            }
          }
        })
        
      }
    
   
  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.onMineInfo();
  },
  onMineInfo: function () {
    var that = this;
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toHomeUser.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        id: app.globalData.userId,
      },
      success: function (res) {
       // console.log(res);
        that.setData({
          bankCardName: res.data.bankcardInfoVo.bankCardName,
          bankCardNumber: res.data.bankcardInfoVo.bankCardNumber,
          bankCard: res.data.bankcardInfoVo.bankCardNumber.substr(-4),
          payStatus: res.data.userVo.payStatus,
          wxType: res.data.userVo.wxType,
          phone: res.data.userVo.telephone
        });
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },

  onFocusOne: function (e) {
    var that = this;
    var inputValue = e.detail.value.trim();
    that.setData({
      ValueOne: inputValue,
    });
    if (e.detail.value.length >= 6) {
      that.setData({ ifShowPasswordTips: false });
      that.onSubmit();
    }
  },

  onTapOne: function (e) {
    console.log(e);
    console.log(e.currentTarget.dataset.index);
    var that = this;
    that.setData({
      isFocusOne: true,
    })

  },

  onRightCancel: function () {
    var that = this;
    that.setData({ ValueOne: "", ifShowPasswordTips: false });
  },

  //忘记密码
  goCheckSetPaymentPassword: function () {
    var that = this;
    var wxType = that.data.wxType;
    var phone = that.data.phone;
    wx.navigateTo({
      url: '../mine/checkSetPaymentPassword?wxType=' + wxType + '&phone=' + phone,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})